package com.detrum.sportcamera.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.detrum.bean.ParamsItem;
import com.detrum.sportcamera.R;
import com.detrum.sportcamera.adapter.RatiosAdapter;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link RatiosFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link RatiosFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RatiosFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    // TODO: Rename and change types of parameters
    private int mParam1;

    private OnFragmentInteractionListener mListener;

    private TextView ratios_tv;
    private GridView ratios_gv;
    private RatiosAdapter ratiosAdapter;
    private List<ParamsItem> itemList;
    private int flag = 2;

    public RatiosFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment RatiosFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static RatiosFragment newInstance() {
        RatiosFragment fragment = new RatiosFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ratios, container, false);
        ratios_tv = view.findViewById(R.id.ratios_tv);
        ratios_gv = view.findViewById(R.id.ratios_gv);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ratiosAdapter = new RatiosAdapter(getActivity(),mParam1-1);
        ratios_gv.setAdapter(ratiosAdapter);
        ratios_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mListener.selectItem(flag,position+1);
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void setParamsSuccess(int ratiosMode){
        ratiosAdapter.setPosition(ratiosMode-1);
    }

    public void setmParam1(int mParam1){
        this.mParam1 = mParam1;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
        void selectItem(int flag,int num);
    }
}
